package com.suneee.quartz;

import java.util.Map;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Quartz 任务管理类
 * @author shangwen
 *
 */
public class QuartzManager {

	private static final Logger logger = LoggerFactory.getLogger(QuartzManager.class);

	// private StdScheduler scheduler;

	private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory();

	/**
	 * 添加一个定时任务
	 * 
	 * @param jobName 任务名
	 * @param jobGroupName 任务组名
	 * @param triggerName 触发器名
	 * @param triggerGroupName 触发器组名
	 * @param jobClass 任务
	 * @param time 时间设置
	 */
	public static void addJob(String jobName, String jobGroupName,String triggerName, String triggerGroupName, Class jobClass,
			String time,Map<String,String> jobMap) {
		try {
			Scheduler sched = gSchedulerFactory.getScheduler();
			JobDetail jobDetail = new JobDetail(jobName, jobGroupName, jobClass);// 任务名，任务组，任务执行类
			if(jobMap != null){
				jobDetail.getJobDataMap().putAll(jobMap);
			}
			// 触发器
			CronTrigger trigger = new CronTrigger(triggerName, triggerGroupName);// 触发器名,触发器组
			trigger.setCronExpression(time);// 触发器时间设定
			sched.scheduleJob(jobDetail, trigger);
			sched.start();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

	/**
	 * 修改任务的触发时间
	 * @param triggerName 触发器名
	 * @param triggerGroupName 触发器组名
	 * @param time 时间设置
	 */
	public static void modifyJobTime(String triggerName, String triggerGroupName, String time) {
		try {
			Scheduler sched = gSchedulerFactory.getScheduler();
			CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerName,triggerGroupName);
			if (trigger == null) {
				return;
			}
			String oldTime = trigger.getCronExpression();
			if (!oldTime.equalsIgnoreCase(time)) {
				CronTrigger ct = (CronTrigger) trigger;
				// 修改时间
				ct.setCronExpression(time);
				// 重启触发器
				sched.resumeTrigger(triggerName, triggerGroupName);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}
	
	/**
	 * 删除定时任务
	 * @param jobName 任务名
	 * @param jobGroupName 任务组名
	 * @param triggerName 触发器名
	 * @param triggerGroupName 触发器组名
	 */
	public static void removeJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName) {
	    try {
		      Scheduler sched = gSchedulerFactory.getScheduler();
		      sched.pauseTrigger(triggerName, triggerGroupName);// 停止触发器
		      sched.unscheduleJob(triggerName, triggerGroupName);// 移除触发器
		      sched.deleteJob(jobName, jobGroupName);// 删除任务
	    } catch (Exception e) {
	    	logger.error(e.getMessage(), e);
	    }
    }

	/**
	 * 启动所有定时任务
	 */
    public static void startJobs() {
	    try {
	    	Scheduler sched = gSchedulerFactory.getScheduler();
	    	sched.start();
	    } catch (Exception e) {
	    	logger.error(e.getMessage(), e);
	    }
    }
	
    /**
     * 关闭所有定时任务
     */
    public static void shutdownJobs() {
        try {
           Scheduler sched = gSchedulerFactory.getScheduler();
           if (!sched.isShutdown()) {
              sched.shutdown();
           }
        } catch (Exception e) {
        	logger.error(e.getMessage(), e);
        }
    }
	
    /**
     * 检测job是否已存在
     * @param jobName job名称
     * @param gropName job所属组
     * @return
     * @throws Exception
     */
	public static Boolean checkIsExisted(String jobName,String gropName) throws Exception{
		Scheduler sched = gSchedulerFactory.getScheduler();
		JobDetail jobDetail = sched.getJobDetail(jobName, gropName);
		if(jobDetail != null)
			return true;
		else
			return false;
	}

}
